package com.crimsec.utils.dinfo.impl.xml;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "files")
public class NewsIndex {

    @XmlType(name = "see-more")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class CollectionPointer {

        public String collectionName;
        public int itemsCount;
        public String collectionInfo;

        public CollectionPointer(String colName, String colInfo, int iCount) {
            this.collectionInfo = colInfo;
            this.collectionName = colName;
            this.itemsCount = iCount;

        }

        public CollectionPointer() {
        }
    }

    public static class CollectionAdapter extends XmlAdapter<CollectionPointer, Collection<NewsItem>> {

        @Override
        public Collection<NewsItem> unmarshal(CollectionPointer v) throws Exception {
            return new ArrayList<NewsItem>();
        }

        @Override
        public CollectionPointer marshal(Collection<NewsItem> v) throws Exception {
            return new CollectionPointer("news-items", "some looong info", v.size());
        }
    }
    private String encodind;
    private URI baseUri;
    //
    private Collection<NewsItem> newsItems;

    public String getEncodind() {
        return encodind;
    }

    public void setEncodind(String encodind) {
        this.encodind = encodind;
    }

    public URI getBaseUri() {
        return baseUri;
    }

    public void setBaseUri(URI baseUri) {
        this.baseUri = baseUri;
    }

    @XmlElementWrapper(name="items")
    //@XmlElementRef(name = "see-more")
    @XmlJavaTypeAdapter(CollectionAdapter.class)
    public Collection<NewsItem> getNewsItems() {
        return newsItems;
    }

    public void setNewsItems(Collection<NewsItem> newsItems) {
        this.newsItems = newsItems;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.encodind != null ? this.encodind.hashCode() : 0);
        hash = 97 * hash + (this.baseUri != null ? this.baseUri.hashCode() : 0);
        hash = 97 * hash + (this.newsItems != null ? this.newsItems.size() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final NewsIndex other = (NewsIndex) obj;
        if ((this.encodind == null) ? (other.encodind != null) : !this.encodind.equals(other.encodind)) {
            return false;
        }
        if (this.baseUri != other.baseUri && (this.baseUri == null || !this.baseUri.equals(other.baseUri))) {
            return false;
        }

        if (this.newsItems != other.newsItems && (this.newsItems == null || !this.newsItems.equals(other.newsItems))) {
            return false;
        }
        return true;
    }
}
